<?php

namespace app\admin\controller\yunfood\printer;

use addons\yunfood\library\Printer;
use addons\yunfood\model\Shop;
use app\admin\model\yunfood\PrinterDevice;
use addons\yunfood\library\Backend;

/**
 * 设备
 */
class Device extends Backend
{
    /**
     * @var PrinterDevice
     */
    protected $model;

    protected $shopField = 'shop_ids';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new PrinterDevice();

        // 关联门店
        $this->view->assign('shopId', $this->shopId);
        $this->assignconfig('shopId', $this->shopId);

        // 门店
        $shopList = Shop::getOptionList($this->getShopWhere(null, 'id'), true, false);
        $this->view->assign('shopList', $shopList);
        $this->assignconfig('shopList', $shopList);

        // 品牌
        $this->view->assign('brandList', $this->model::getBrandList());
        $this->assignconfig('brandList', $this->model::getBrandList());

        // 场景
        $this->view->assign('sceneList', $this->model::getSceneList());
        $this->assignconfig('sceneList', $this->model::getSceneList());

        // 状态
        $this->view->assign('statusList', $this->model::getStatusList());
        $this->assignconfig('statusList', $this->model::getStatusList());
    }

    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            // 转发到selectMenu
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            // 重载查询条件
            $filter = $this->request->get('filter');
            $op = $this->request->get('op');
            list($filter, $op) = $this->formatFilterOp($filter, $op);
            $this->request->get(['filter' => $filter, 'op' => $op]);

            // 通用查询条件
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            // 查询门店
            $where = $this->getShopWhere($where, 'shop_ids');
            $list = $this->model->where($where)->order($sort, $order)->limit($offset, $limit)->paginate($limit);
            foreach ($list->items() as $item) {
                $item->append(['shop_text']);
                if (! $this->superAdmin) {
                    if (empty($item->shop_ids) || strpos($item->shop_ids, ',') !== false) {
                        $item['mute_operate'] = true;
                    }
                }
            }
            $result = array(
                'total' => $list->total(),
                'rows'  => $list->items(),
            );

            return json($result);
        }

        return $this->view->fetch();
    }

    /**
     * 格式化查询条件
     *
     * @param $filter
     * @param $op
     * @return array
     */
    private function formatFilterOp($filter, $op)
    {
        $filter = json_decode($filter, true);
        $op = json_decode($op, true);
        if (array_key_exists('shopId', $filter)) {
            if ($filter['shopId'] == 0) {
                $filter['shop_ids'] = '';
                $op['shop_ids'] = '=';
            } else {
                $filter['shop_ids'] = $filter['shopId'] . '';
                $op['shop_ids'] = 'find_in_set';
            }
            unset($filter['shopId'], $op['shopId']);
        }

        return [json_encode($filter), json_encode($op)];
    }

    /**
     * 打印测试
     */
    public function testPrint($ids = null)
    {
        $row = $this->model::get($ids);
        if (! $row) {
            $this->error(__('No Results were found'));
        }
        $this->authToShop($row['shop_ids']);

        try {
            // 绑定飞鹅打印机
            if ($row->brand === PrinterDevice::BRAND_FEIE) {
                Printer::bindFeieDevice($row);
            }
            Printer::printContent($row->brand, '打印测试', $row);
        } catch (\Throwable $e) {
            $this->error($e->getMessage());
        }

        $this->success();
    }

    public function recyclebin()
    {
        return;
    }

    public function destroy($ids = null)
    {
        return;
    }

    public function restore($ids = null)
    {
        return;
    }
}
